//
//  AppUIDefine.swift
//  LittleThink
//
//  Created by wei.feng on 2023/12/1.
//

import Foundation
import UIKit

//MARK: Utility
//屏幕宽度
var SCREEN_WIDTH: CGFloat {
    let width = UIScreen.main.bounds.width
    let height = UIScreen.main.bounds.height
    return width > height ? height : width
}
//屏幕高度
var SCREEN_HEIGHT: CGFloat {
    let width = UIScreen.main.bounds.width
    let height = UIScreen.main.bounds.height
    return width > height ? width : height
}
//屏幕比例
var ScreenScale: CGFloat { return min(SCREEN_WIDTH / 375.0, SCREEN_HEIGHT / 375.0) }

//根据当前屏幕宽度得到宽度
func pixw(p: CGFloat, flag: Int = 1) -> CGFloat {
    let value = (SCREEN_WIDTH / 375.0) * p
    if flag == 1 {
        return round(value)
    } else if flag == 2 {
        return floor(value)
    } else if flag == 3 {
        return ceil(value)
    }
    return value
}

//导航
var navigationHeight: CGFloat { return topSafeAreaHeight + 44 }

//tabBar的高度
var tabBarHeight: CGFloat { return bottomSafeAreaHeight + 49 }

//安全区高度
var bottomSafeAreaHeight: CGFloat { return windownSafeAreaInsets.bottom }
var topSafeAreaHeight: CGFloat { return windownSafeAreaInsets.top }

var windownSafeAreaInsets: UIEdgeInsets {
    if wSafeAreaInsets != .zero {
        return wSafeAreaInsets
    }
    if Thread.isMainThread, let windown = GkeyWindow() {
        wSafeAreaInsets = windown.safeAreaInsets
        return wSafeAreaInsets
    }
    return UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
}
private var wSafeAreaInsets: UIEdgeInsets = .zero


func GkeyWindow() -> UIWindow? {
    return UIApplication.shared.connectedScenes
//        .filter { $0.activationState == .foregroundActive || $0.activationState == .foregroundInactive }
        .map { $0 as? UIWindowScene }
        .compactMap { $0 }
        .first?.windows
        .filter { $0.isKeyWindow }.first
}

func GRootVc() -> UIViewController? {
    return GkeyWindow()?.rootViewController
}

//MARK: Colors
extension UIColor {
 
    class func appMainColor(_ alpha: Float = 1.0) -> UIColor {
        return UIColor(hexString: "#FF1213", alpha: alpha)!
    }

    class func ltTitleColor(_ alpha: Float = 1.0) -> UIColor {
        return UIColor(hexString: "#001213", alpha: alpha)!
    }

    class func ltTextColor(_ alpha: Float = 1.0) -> UIColor {
        return UIColor(hexString: "#001213", alpha: alpha)!
    }

}

//MARK: Font
extension UIFont {
    
    class func fontBold(size: CGFloat) -> UIFont {
        let font = UIFont.init(name: "Poppins", size: size)
        return font ?? UIFont.boldSystemFont(ofSize: size)
    }
        
    class func fontRegular(size: CGFloat) -> UIFont {
        let font = UIFont.init(name: "Poppins-Regular", size: size)
        return font ?? UIFont.systemFont(ofSize: size)
    }
    
}

extension UIFont {
    
    class func ltTitleFont() -> UIFont {
        return UIFont.fontBold(size: 16)
    }

    class func ltTextFont() -> UIFont {
        return UIFont.fontRegular(size: 14)
    }

    class func ltTimeFont() -> UIFont {
        return UIFont.fontRegular(size: 11)
    }

}

